<?php
namespace Borrow\Controller;

use Base\Controller\AdminBaseController;

class BorrowController extends AdminBaseController {
    
	protected $borrowModel;
	
	function _initialize() {
		parent::_initialize();
		
		$this->borrowModel = D("Borrow/Borrow");
	}
	
	/******************************** JSON *********************************/
	/** 添加信息 */
	public function save() {
	}
    
	/** 查看所有列表 */
	public function dataList() {
		$dataList = $this->borrowModel->dataList($_GET);
	    $this->ajaxReturn($dataList);
	}
    
	/** 查看某信息 */
	public function find() {
		$id = $_GET['id'];
		
		$borrow = $this->borrowModel->where(array("id" => $id))->find();
		if (!$borrow) {
			$data['code'] = 499;
			$data['msg'] = '未找到目标！';
			$this->ajaxReturn($data);
		}
		
		// bar
		$borrow['bar'] = $borrow['province_id'] . "," . $borrow['address'];
		
	    $data['code'] = 0;
	    $data['msg'] = 'OK';
	    $data['obj'] = $borrow;
	    $this->ajaxReturn($data);
	}
    
	/** 修改某信息 */
	public function update() {
		$id = $_GET['id'];
		$borrowJson = file_get_contents('php://input');
		$borrow = json_decode($borrowJson, true);

		// update
		$borrow['url'] = '';											// update reset
		$borrow['status'] = 0;										// update reset
		$borrow['update_time'] = date("Y-m-d H:i:s");

		// bar
		list($borrow['province_id'], $borrow['address']) = split(',', $borrow['bar']);
		
		// 执行更新
		$result = $this->borrowModel->where(array("id" => $id))->save($borrow);
		if (false === $result) {
			$data['code'] = 499;
			$data['msg'] = '编辑失败！';
			$this->ajaxReturn($data);
		}

		$updatedBorrow = $this->borrowModel->where(array("id" => $id))->find();
		
		$data['code'] = 0;
		$data['msg'] = '编辑成功！';
		$data['obj'] = $updatedBorrow;
		$this->ajaxReturn($data);
	}
    
	/** setStatus */
	public function setStatus() {			// statusFlag	0: waiting , 1: confirmed, 2: returned, 3: canceled
		$id = $_GET['id'];
		$status = $_GET['status'];

		$borrow = $this->borrowModel->where(array("id" => $id))->find();
		if (!$borrow) {
			$data['code'] = 499;
			$data['msg'] = '未找到目标！';
			$this->ajaxReturn($data);
		}
		
		if ($status == 1) {										// 处理确认
			if ($borrow['status'] != 0) {
    			$data['code'] = 499;
    			$data['msg'] = '借阅失败，不是待确认状态！';
    			$this->ajaxReturn($data);
			}
            
    		// 更新$borrow
    		$borrow['status'] = 1;
    		$borrow['start_time'] = date("Y-m-d H:i:s");
    		$result = $this->borrowModel->where(array("id" => $id))->save($borrow);
            
			$data['code'] = 0;
			$data['msg'] = '处理成功！';
			$data['obj'] = $borrow;
			$this->ajaxReturn($data);
		} else if ($status == 2) {								// 处理还书
			if ($borrow['status'] != 1) {
    			$data['code'] = 499;
    			$data['msg'] = '借阅失败，不是借阅状态！';
    			$this->ajaxReturn($data);
			}
			
			// 图书剩余 + 1
    		M('Book')->where(array("id" => $borrow['book_id']))->setInc('remain_count', 1);

			// 会员卡当前借阅 - 1
    		M('Card')->where(array("id" => $borrow['card_id']))->setDec('cur_borrow_count', 1);

    		// 更新$borrow
    		$borrow['status'] = 2;
    		$borrow['end_time'] = date("Y-m-d H:i:s");
    		$result = $this->borrowModel->where(array("id" => $id))->save($borrow);
            
			$data['code'] = 0;
			$data['msg'] = '处理成功！';
			$data['obj'] = $borrow;
			$this->ajaxReturn($data);
		} else if ($status == 3) {								// 处理取消
			if ($borrow['status'] != 0) {
    			$data['code'] = 499;
    			$data['msg'] = '借阅失败，不是待确认状态！';
    			$this->ajaxReturn($data);
			}
			
			// 图书剩余 + 1
    		M('Book')->where(array("id" => $borrow['book_id']))->setInc('remain_count', 1);

			// 会员卡当前借阅 - 1
    		M('Card')->where(array("id" => $borrow['card_id']))->setDec('cur_borrow_count', 1);

    		// 更新$borrow
    		$borrow['status'] = 3;
    		$result = $this->borrowModel->where(array("id" => $id))->save($borrow);
            
			$data['code'] = 0;
			$data['msg'] = '取消成功！';
			$data['obj'] = $borrow;
			$this->ajaxReturn($data);
		}
		
		$data['code'] = 499;
		$data['msg'] = '处理失败，未知操作！';
		$this->ajaxReturn($data);
	}
	
	/** 删除某信息 */
	public function delete() {
		$id = $_GET['id'];
		
		$borrow = $this->borrowModel->where(array("id" => $id))->find();
		if (!$borrow) {
			$data['code'] = 499;
			$data['msg'] = '未找到目标！';
			$this->ajaxReturn($data);
		}
		
		if (!$this->borrowModel->delete($id)) {
			$data['code'] = 499;
			$data['msg'] = '删除失败！';
			$this->ajaxReturn($data);
		}
        
		$data['code'] = 0;
		$data['msg'] = '删除成功！';
		$this->ajaxReturn($data);
	}
    
	/******************************** HTML *********************************/
	// index
    public function index() {
		$this->display(':borrow');
		return;
	}
	
}